/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO integrate the <script> tag injection directly from flash
package martian.m4gic.services
{
	import flash.display.*;
	import flash.events.*;
	import flash.external.ExternalInterface;
	
	public class GA
	{
		public function GA() { super(); }
		
		/* service name */
		static private const NAME:String = "Google Analytics";
		
		/* service properties */
		static private var tracker:String;
		
		/* service initialization */
		static private function _register(args:Object = null):void
		{
			tracker = (args.tracker != undefined) ? args.tracker : "";
			if (tracker == "") { trace('5:You must specify a valid tracker value in args object'); }
			else if (js) { js = Javascript.call('_gat != undefined'); }
		}
		
		static private function _release(args:Object = null):void {}
		static private function _activate(args:Object = null):void
		{
			if (Javascript.available)
			{
				Javascript.call("var GA = _gat._getTracker('" + tracker + "');");
				Javascript.call("GA._initData();");
			}
		}
		static private function _desactivate(args:Object = null):void {}
		
		/* service methods */
		static public function track(page:String):void
		{
			if (available && Javascript.available && page != "")
			{
				call("GA._trackPageview('" + page + "');");
			}
		}
		
		static public function event(category:String, action:String, label:String = "", value:Number = NaN):void
		{
			if (available && Javascript.available && category != "" && action != "")
			{
				var cmd:String = "GA._trackEvent('" + category + "', '" + action + "'";
					if (label != "") { cmd += ", '" + label + '"; }' }
					if (!isNaN(value)) { cmd += ", " + value; }
					cmd += ");";
					
				Javascript.call(cmd);
			}			
		}
		
		/* inheritance */
		static private var reference:Sprite;
		static private var dispatch:EventDispatcher;
		static private var stage:Stage;
		
		static private var registered:Boolean = false;
		static private var activated:Boolean = false;
			static public function get available():Boolean { return (registered && activated); }
		
		static public function register(container:Sprite, args:Object = null):void
		{
			if (!registered)
			{
				reference = container;
				dispatch = new EventDispatcher();
				stage = reference.stage;
				
				_register(args);
				
				registered = true;
				
				if (args != null) { if (args.active != undefined) { if (args.active == true) { activate(); } } }
			}
			else { trace("6:", NAME, "already registered."); }
		}
		
		static public function release(args:Object = null):void
		{
			if (registered && !activated)
			{
				stage = null;
				dispatch = null;
				reference = null;
				
				_release(args);
				
				registered = false;
			}
			else { trace("6:", NAME, "already released."); }
		}
		
		static public function activate(args:Object = null):void
		{
			if (registered && !activated)
			{
				activated = true;
				_activate(args);
			}			
		}
		
		static public function desactivate(args:Object = null):void
		{
			if (registered && activated)
			{
				activated = false;
				_desactivate(args);
			}
		}
		
		static private function dispatchEvent(event:Event):void
		{
			dispatch.dispatchEvent(event);
		}
		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			dispatch.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			dispatch.removeEventListener(type, listener, useCapture);
		}
	}
}